<?php

class ConsoleAction extends Action {

    function _initialize() {

        if (!in_array(strtolower(ACTION_NAME), array('login', 'checkloginfromoldside'))) {
            $s = session('info');
            if (empty($s)) {
// 				$this->redirect("/",array('err'=>'error03'));
                header('location:/?err=error03');
            }
        }
    }

    function login() {

//     	if(session("info")){
//     		$this->redirect("/Console/index/");
//     		return ;
//     	}
        //t_admin
        $ob = M("yq_admin");
		
        if ($_POST) {

            if (session("verify") != md5($_POST['verifycode'])) {
                $this->assign('err', '验证码错误');
            } else {
                
                $info = $ob->where(array('account' => $_POST['account']))->find();
                if ($info['password'] == md5($_POST['password'])) {
                    session("info", $info);

                    //登录成功之后查询权限
                    $ob = M("yq_role_auth");
                    $menuFirst = $ob->join("yq_menu m on m.tid = yq_role_auth.menuid ")->field("m.tid,m.name,m.pid,m.module,m.action,m.data,m.remark,m.ordid,m.display,m.moduleid")->where(array("yq_role_auth.roleid"=>$info['roleid'],"m.display"=>"1","m.pid"=>"0"))->order("m.ordid")->select();

                    $menuSecond = $ob->join("yq_menu m on m.tid = yq_role_auth.menuid and m.display = '1' and m.pid != '0' ")->field("m.tid,m.name,m.pid,m.module,m.action,m.data,m.remark,m.ordid,m.display,m.moduleid")->where(array("yq_role_auth.roleid"=>$info['roleid'],"m.display"=>"1","m.pid"=>array("neq","0")))->order("m.ordid")->select();

                    session("menuFirst", $menuFirst);
                    session("menuSecond", $menuSecond);
                    $this->assign("menuFirst", $menuFirst);
                    $this->assign("menuSecond", $menuSecond);
                    new SecurityAction();
                    $this->display("Console/blank");
                    // $this->redirect("/Console/index");
                    return;
                } else {
                    $this->assign('err', '用户名密码错误');
                }
            }
        }

        if ($_GET['err'] == 'error03') {
            $this->assign('err', '登录超时，请重新登录。');
        }
        $this->display("Console/login");
    }

    function index() {
    	
    	new SecurityAction();
        $this->display("Console/blank");
    }

    function logout() {
        session("info", null);
        session_destroy();
        $this->redirect("/");
    }

    

}
